package com.chuck.learning.线程间通讯;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;

public class ThreadTrun extends AbstractQueuedSynchronizer {

    static volatile int state = 0;
    static AtomicInteger atomicInteger = new AtomicInteger(0);
    static AtomicInteger a = new AtomicInteger(0);
    static AtomicInteger b = new AtomicInteger(0);

    static Object LOCK = new Object();

    static ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(1);

    public static void main1(String[] args) {
        new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(1000);
                    arrayBlockingQueue.put("1");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程A");
            }
        }).start();

        new Thread(() -> {
            while (true) {
                try {
                    arrayBlockingQueue.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程B");
            }
        }).start();
    }


    public static void main(String[] args) {
        Runnable r = () -> {

            synchronized (LOCK) {
                while (state != 0) {
                    try {
                        LOCK.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("state = 0 执行");
                    state=1;
                    LOCK.notifyAll();
                }

            }

        };

        Runnable r2 = () -> {
            synchronized (LOCK) {
                while (state != 1) {
                    try {
                        LOCK.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("state = 1 执行");
                    state= 0;
                    LOCK.notifyAll();
                }

            }
        };
        Thread thread2 = new Thread(r2);
        thread2.start();
        Thread thread1 = new Thread(r);
        thread1.start();



    }


    public static void main2(String[] args) {
        ThreadTrun threadTrun = new ThreadTrun();
        new Thread(() -> {
            while (atomicInteger.get() <= 100) {
                if (threadTrun.compareAndSetState(0, 1)) {
                    a.getAndIncrement();
                    atomicInteger.getAndIncrement();
                }
            }
        }).start();

        new Thread(() -> {
            while (atomicInteger.get() <= 100) {
                if (threadTrun.compareAndSetState(1, 0)) {
                    b.getAndIncrement();
                    atomicInteger.getAndIncrement();
                }
            }
        }).start();
        while (atomicInteger.get() <= 100) {
        }
        System.out.println(a + "        " + b);
    }

}
